package com.hjq.mallproduct.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.hjq.mallproduct.entity.BrandEntity;
import com.hjq.mallproduct.entity.CategoryEntity;
import com.hjq.mallproduct.entity.vo.BrandVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hjq.mallproduct.entity.CategoryBrandRelationEntity;
import com.hjq.mallproduct.service.CategoryBrandRelationService;
import com.hjq.common.utils.PageUtils;
import com.hjq.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author Hjq
 * @email 1484397309@qq.com.com
 * @date 2022-05-24 14:18:33
 */
@RestController
@RequestMapping("mallproduct/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;


    /**
     * 获取品牌关联的分类
     */
    @GetMapping("/brands/list")
    // @RequiresPermissions("mallproduct:categorybrandrelation:delete")
    public R getBrandlistByCatId(@RequestParam("catId") Long catId){
        List<BrandEntity> brandlist = categoryBrandRelationService.getBrandlistByCatId(catId);

        List<BrandVO> relationEntities = brandlist.stream().map((item) -> {
            BrandVO categoryBrandRelationEntity = new BrandVO();
            BeanUtils.copyProperties(item, categoryBrandRelationEntity);
            categoryBrandRelationEntity.setBrandName(item.getName());
            return categoryBrandRelationEntity;
        }).collect(Collectors.toList());
        return R.ok().put("data",relationEntities);
    }

    /**
     * 获取品牌关联的分类
     */
    @GetMapping("/catelog/list")
    // @RequiresPermissions("mallproduct:categorybrandrelation:delete")
    public R getcateloglistByBrandId(@RequestParam("brandId") Long brandId){
        List<CategoryBrandRelationEntity> entities = categoryBrandRelationService.getcateloglistByBrandId(brandId);
        return R.ok().put("data",entities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("mallproduct:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("mallproduct:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     *
     * 保存品牌关联的分类
     *
     */
    @RequestMapping("/save")
    //@RequiresPermissions("mallproduct:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.save(categoryBrandRelation.getBrandId()
                                          ,categoryBrandRelation.getCatelogId());

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("mallproduct:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("mallproduct:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
